#include <iostream>
#include <unistd.h>
#include <string>
#include "opcodes.h"
#include "assembler.h"

// Prototypes
void    Usage(void);
void    Version(void);

#define MAJ_VER     1
#define MIN_VER     0
#define VERSION_NOTES "release candidate"

static MachineEmulator *    vMachine;
static Assembler *          assembler;

int main(int argc, char ** argv)
{
    u32     codeLen;
    u16     initialSP;
    int     opt;
    bool    bVerbose = true;
    std::string codeFile, outFile;
    
    while ((opt = getopt(argc,argv,"vVhc:o:")) != -1)
    {
        switch(opt)
        {
            case 'c':
                codeFile = optarg;
                break;
            case 'o':
                outFile = optarg;
                break;
            case 'v':
                bVerbose = true;
                break;
            case 'V':
                Version();
                return 0;
            case 'h':
            default:
                Usage();
                return 0;
        }
    }
    vMachine    = new MachineEmulator();
    assembler   = new Assembler("test.asm",vMachine);

    bool bHasErrors = assembler->Assemble(codeLen,initialSP);
    delete assembler;

    vMachine->ListCode(codeLen);
    vMachine->Interpret(codeLen,initialSP);
    delete vMachine;

    
    return 0;
}

void Usage()
{
    Version();
    std::cout << "Usage: munix-as [options] -c <src> -o <binary>" << std::endl;
    std::cout << "Where:" << std::endl;
    std::cout << "\t-h This help" << std::endl;
    std::cout << "\t-V Version info" << std::endl;
    std::cout << "\t-v Verbose format" << std::endl;
    std::cout << "\t-c <srcfile> PXE assembler" << std::endl;
    std::cout << "\t-o <outfile> PXE executable" << std::endl;
    std::cout << std::endl;
}

void Version()
{
    std::cout << std::endl;
    std::cout << "Version: munix-as (" << MAJ_VER << "." << MIN_VER << "." <<
            BUILD_NUM << ") " << VERSION_NOTES << std::endl;
    std::cout << "Build number: " << BUILD_NUM << std::endl;
    std::cout << "Copyright 2009-2011 OpenHouseware, LLC. All Rights Reserved." << std::endl;
    std::cout << "This is open source software licensed under the CDDL" << std::endl;
    std::cout << std::endl;
}